package com.sesum;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class Login extends Sesum{
	
	private static final int LOGIN_FAILED = 0;
	private static final int REGISTER_FAILED = 1;

	@Override
	protected Dialog onCreateDialog(int id) {
		
		switch (id) {
		case LOGIN_FAILED:
			return new AlertDialog.Builder(this)

			.setTitle("Login Failed!")

			.setMessage("Sorry")

			.setNeutralButton("Aceptar",

			new DialogInterface.OnClickListener() {

			public void onClick(DialogInterface dialog,

			int which) {

			}

			}).create();

		case REGISTER_FAILED:
			return new AlertDialog.Builder(this)

			.setTitle("Login Failed!")

			.setMessage("Sorry")

			.setNeutralButton("Aceptar",

			new DialogInterface.OnClickListener() {

			public void onClick(DialogInterface dialog,

			int which) {

			}

			}).create();

	
		default:
			break;
		}
		return null;
	}
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
        	// Get the EditText and Button References
        final EditText etUsername = (EditText)findViewById(R.id.username);
        final EditText etPassword = (EditText)findViewById(R.id.password);
        Button btnLogin = (Button)findViewById(R.id.login_button);
        Button btnRegister = (Button)findViewById(R.id.register_button);
        Button btnCancel = (Button)findViewById(R.id.cancel_button);
//        	lblResult = (TextView)findViewById(R.id.result);
        
        
        	btnLogin.setOnClickListener(new OnClickListener() {
        		@Override
				public void onClick(View v) {
    		        // Check Login
        			boolean result = login(etUsername.getText().toString(), etPassword.getText().toString());
        			if (result){
        				Intent intent = new Intent(getApplicationContext(), Activate.class);
        				startActivity(intent);
        			}else{
        				showDialog(LOGIN_FAILED);
        			}
        			
				}
			});
        	
        	btnRegister.setOnClickListener(new OnClickListener() {
        		@Override
				public void onClick(View v) {
    		        // Check Login
    				Intent intent = new Intent(getApplicationContext(), Register.class);
    				startActivity(intent);
        			
				}
			});
        	
        	btnCancel.setOnClickListener(new OnClickListener() {
        		@Override
				public void onClick(View v) {
    		        finish();
        			
				}
			});
        	
        	
        

    }

}
